/*
 * Copyright 2008-2009 Daniel Cachapa <cachapa@gmail.com>
 * 
 * This program is distributed under the terms of the GNU General Public Licence Version 3
 * The licence can be read in its entirety in the LICENSE.txt file accompaning this source code,
 * or at: http://www.gnu.org/copyleft/gpl.html
 * 
 * This file is part of WeightWatch.
 *
 * WeightWatch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * WeightWatch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the WeightWatch source code. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package net.cachapa.weightwatch.ui.components;

import com.sun.lwuit.Command;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Form;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.DefaultListModel;
import java.util.Enumeration;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;

public class FileChooser extends Form implements ActionListener {

	private String path;
	private List list;
	private Command backCommand;

	public FileChooser() {
//		path = System.getProperty("fileconn.dir.photos");
		path = "/";
		this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
		this.setScrollable(false);
		this.setFocusable(false);

		list = new List();
		list.setNumericKeyActions(false);
//		list.setListCellRenderer(new DatabaseListCellRenderer());
		list.setFixedSelection(List.FIXED_NONE_CYCLIC);
		list.setFireOnClick(true);
		list.addActionListener(this);
		populateList(path);
		this.addComponent(list);
		
		backCommand = new Command("back");
		this.addCommand(backCommand);
		this.setBackCommand(backCommand);
		this.setCommandListener(this);
	}
	
	private void populateList(String path) {
		this.setTitle(path);
		list.setModel(new DefaultListModel());	// clear the list
		if (path.equals("/")) {
			listRoots();
		}
		else {
			listDirectory(path);
		}
		this.repaint();
	}
	
	private String parentDirectory(String path) {
		String result = path;
		int size = path.length();
		for (int i = size - 2; i >= 0; i--) {
			if (path.charAt(i) == '/') {
				result = path.substring(0, i + 1);
				break;
			}
		}
		return result;
	}
	
	private void listRoots() {
		Enumeration rootList = FileSystemRegistry.listRoots();
		while (rootList.hasMoreElements()) {
			String rootName = (String) rootList.nextElement();
			list.addItem(rootName);
		}
	}
	
	private void listDirectory(String path) {
		try {
			list.addItem("..");
			FileConnection fc = (FileConnection) Connector.open("file://" + path);
			Enumeration filelist = fc.list();
			while (filelist.hasMoreElements()) {
				String fileName = (String) filelist.nextElement();
//				fc = (FileConnection) Connector.open(path + fileName);
//				if (fc.isDirectory()) {
//					System.out.println("\tDirectory Name: " + fileName);
//				} else {
//					System.out.println("\tFile Name: " + fileName +
//						"\tSize: " + fc.fileSize());
//				}
				list.addItem(fileName);
			}
			fc.close();
		} catch (Exception ex) {
			Dialog.show("Exception", ex.getMessage(), "Ok", "Ok");
			ex.printStackTrace();
		}
	}

	public void actionPerformed(ActionEvent ae) {
		Object source = ae.getSource();
		if (source == list) {
			String filename = (String) list.getSelectedItem();
			if (filename.equals("..")) {
				path = parentDirectory(path);
			}
			else {
				path += filename;
			}
			populateList(path);
		}
		else if (source == backCommand) {
			if (!path.equals("/")); {
				path = parentDirectory(path);
				populateList(path);
			}
		}
	}
}
